#include "stdafx.h"
#include "RFComDeviceNet.h"
#include "include\RFComDevice.h"

using namespace RFDeviceNet;
using namespace System::Runtime::InteropServices;

RFComDeviceNet^ RFComDeviceNet::CreateRFComDevice()
{
	RFDevice::RFComDevice* device = new RFDevice::RFComDevice();
	RFComDeviceNet^ dev = gcnew RFComDeviceNet(device);
	return dev;
}

RFComDeviceNet::RFComDeviceNet(RFDevice::RFComDevice* device) : RFDeviceObjectNet(device)
{
	_rfComDevice = device;	
}

RFComDeviceNet::RFComDeviceNet()
{
	_rfComDevice = new RFDevice::RFComDevice();	
}

RFComDeviceNet::~RFComDeviceNet()
{
	_rfComDevice->~RFComDevice();
	_rfComDevice = NULL;
}

bool RFComDeviceNet::OpenPort(String^ port_name, UInt32 speed)
{
	const char* str = (const char*)(void*)Marshal::StringToHGlobalAnsi(port_name);	
	bool ret = _rfComDevice->OpenPort(str, speed);
	return ret;
}

bool RFComDeviceNet::ClosePort()
{
	return _rfComDevice->ClosePort();
}

bool RFComDeviceNet::ReadParameter(byte address, WORD parameter, DWORD% value)
{
	DWORD* pValue;
	bool ret = _rfComDevice->ReadParameter(address, parameter, pValue);
	if(pValue != NULL)
		value = *pValue;
	return ret;
}

bool RFComDeviceNet::WriteParameter(byte address, USHORT parameter, UInt32 value)
{
	return _rfComDevice->WriteParameter(address, parameter, value);
	
}

bool RFComDeviceNet::ReadCustomParameter(byte address, byte parameterAddress, byte parameterSize, array<byte>^% value)
{
	byte* pValue = new byte[parameterSize];	
	bool ret = _rfComDevice->ReadCustomParameter(address, parameterAddress, parameterSize, pValue);
	if(value->Length != parameterSize)
		value = gcnew array<byte>(parameterSize);
	for(int i = 0; i < parameterSize; i++)
	{
		value[i] = pValue[i];
	}
	return ret;
}

bool RFComDeviceNet::WriteCustomParameter(byte address, byte parameterAddress, byte parameterSize, array<byte>^% value)
{
	byte* pValue = new byte[parameterSize];	
	bool ret = _rfComDevice->WriteCustomParameter(address, parameterAddress, parameterSize, pValue);
	if(value->Length != parameterSize)
		value = gcnew array<byte>(parameterSize);
	for(int i = 0; i < parameterSize; i++)
	{
		value[i] = pValue[i];
	}
	return ret;
}

bool RFComDeviceNet::FlushToFlash(byte address)
{
	return _rfComDevice->FlushToFlash(address);
}

bool RFComDeviceNet::RestoreFromFlash(byte address)
{
	return _rfComDevice->RestoreFromFlash(address);
}

bool RFComDeviceNet::LockResult(byte address)
{
	return _rfComDevice->LockResult(address);
}

bool RFComDeviceNet::Measure(byte address, USHORT% value)
{
	USHORT* pValue = new USHORT;
	bool ret = _rfComDevice->Measure(address, pValue);
	value = *pValue;
	return ret;
}

bool RFComDeviceNet::StartStream(byte address)
{
	return _rfComDevice->StartStream(address);
}

bool RFComDeviceNet::StopStream(byte address)
{
	return _rfComDevice->StopStream(address);
}

bool RFComDeviceNet::GetStreamMeasure(USHORT% value)
{
	USHORT* pValue = new USHORT;
	bool ret = _rfComDevice->GetStreamMeasure(pValue);
	value = *pValue;
	return ret;
}

IntPtr RFComDeviceNet::GetPortHandle()
{
	void* p = _rfComDevice->GetPortHandle();
	return IntPtr(p);
}

bool RFComDeviceNet::CustomCmd(array<byte>^ inData, UInt32 inSize, array<byte>^% outData, UInt32% outSize)
{
	//char* pInData = (char*)(void*)Marshal::StringToHGlobalAnsi(inData);	
	//char* pOutData;
	//DWORD* dwpOutSize;
	char* pinData = new char[inData->Length];
	for(int i = 0; i < inData->Length; i++)
	{
		pinData[i] = inData[i];
	}
	char* poutData;
	DWORD* dwpOutSize = new DWORD;
	bool ret = _rfComDevice->CustomCmd(pinData, inSize, poutData, dwpOutSize);
	outSize = *dwpOutSize;
	outData = gcnew array<byte>(outSize);
	if(poutData != NULL)
	{
		for(int i = 0; i < outSize; i++)
		{
			outData[i] = poutData[i];
		}
	}	
	return ret;
}

bool RFComDeviceNet::ValidateData(array<Byte>^ data, int dataLength)
{
	byte *pData = new byte[data->Length];
	int i = 0;
	for each (byte var in data)
	{
		pData[i] = data[i++];
	}
	bool ret = RFDevice::RFComDevice::ValidateData(pData, dataLength);
	return ret;
}

bool RFComDeviceNet::EthernetOpenPort()
{
	return _rfComDevice->EthernetOpenPort();
}

bool RFComDeviceNet::EthernetSetTimeout(UInt32 timeout)
{
	return _rfComDevice->EthernetSetTimeout(timeout);
}

bool RFComDeviceNet::EthernetClosePort()
{
	return _rfComDevice->EthernetClosePort();
}